package dwz.web.listenerAndInterceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import dwz.persistence.beans.ChongFuDengLu;
import dwz.persistence.beans.ChongFuDengLuExample;
import dwz.persistence.mapper.ChongFuDengLuMapper;
import dwz.web.management.lotto.CommonFields;
/**
 * 后台用户重复登录拦截器
 * @author 
 *
 */
public class BackUserRepeatCheckInterceptor implements HandlerInterceptor{
	@Autowired
	ChongFuDengLuMapper chongFuDengLuMapper;
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		
		
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2) throws Exception {
		String sessionid = arg0.getSession().getId();
		Integer userId = (Integer) arg0.getSession().getAttribute(CommonFields.USERID);
		//逻辑说明 根据userid 和sessionid 来查cfdl这张表 如果发现没有这个信息 就session.invalidate() 然后转向登录页面就ok le 
		ChongFuDengLuExample chongFuDengLuExample = new ChongFuDengLuExample();
		chongFuDengLuExample.createCriteria().andUserIdEqualTo(userId).andSessionIdEqualTo(sessionid).andBFEqualTo(CommonFields.BACKFLAG);
		List<ChongFuDengLu> chongFuDengLus = chongFuDengLuMapper.selectByExample(chongFuDengLuExample);
		if(chongFuDengLus == null){
			arg0.getSession().invalidate();
			arg1.sendRedirect("/management/back/login");
		}
		return true;
	}

}
